﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using pwblog.BusinessObject;
using pwblog.Entities;

namespace pwblog.Web
{
    public partial class DetailsArticle : System.Web.UI.Page
    {
        private string idArticle;
        protected string encodeUrl;
        protected string encodeTitle;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                idArticle = Request.QueryString["id"];
                try
                {
                    // lay thong tin cua bai viet
                    ArticleBo articleBo = new ArticleBo();
                    Article article = articleBo.GetArticleById(Int32.Parse(idArticle));

                    // Title
                    Page.Title = "PWBlog || " + article.Name;

                    // hien thi title, username, datePost
                    lblTitle.Text = article.Name;
                    lblUserName.Text = article.UserName;
                    lblDatePost.Text = article.DatePost;

                    string path = Server.MapPath(@"Articles\" + idArticle + ".txt");
                    StreamReader reader = new StreamReader(path);
                    lblContentsArticle.Text = reader.ReadToEnd();
                    reader.Close();

                    // load comment
                    LoadAllComment();
                }
                catch(Exception)
                {
                    lblContentsArticle.Text = "This article is not contents.";
                }


                encodeUrl = HttpUtility.UrlEncode(Request.Url.AbsoluteUri);
                encodeTitle = HttpUtility.UrlEncode(Page.Title);
            }
        }

        protected void btnComment_Click(object sender, EventArgs e)
        {
            idArticle = Request.QueryString["id"];
            string name = tbName.Text.Trim();
            string comment = tbComment.Text.Trim();
            string dateComment = DateTime.Now.ToString();

            Comment cmt = new Comment();
            cmt.Name = name;
            cmt.Comment1 = comment;
            cmt.IdArticle = Int32.Parse(idArticle);
            cmt.DateComment = dateComment;

            if (new CommentBo().AddComment(cmt))
            {
                // xoa du lieu tren textbox
                tbName.Text = "";
                tbComment.Text = "";

                // update lai comment
                LoadAllComment();
            }
        }

        private void LoadAllComment()
        {
            idArticle = Request.QueryString["id"];
            Comment[] comments = new CommentBo().GetComments(Int32.Parse(idArticle));

            if (comments != null)
            {
                listComment.DataSource = comments;
                listComment.DataBind();
            }
        }
    }
}